using System;
using System.Linq;
using UnityEngine;
using QFramework;

namespace ProjectMyFarmAndShe
{
	public partial class ShopBuyItemPanel : ViewController,IController
	{
		private IShopSystem mSystem;
		private int UpdateDay = 1;
		private int ResetShopItemDataDay = 1;
		
		private void Awake()
		{
			ShopItemTemplate.Hide();

			mSystem = this.GetSystem<IShopSystem>();
		}

		private void OnEnable()
		{
			Content.DestroyChildren();
			
			foreach (var shopItem in mSystem.CanBuyShopItem)
			{
				ShopItemTemplate.InstantiateWithParent(Content)
					.Self(self =>
					{
						var cache = shopItem;
						self.Init(cache);
					})
					.Show();
			}
		}

		private void Start()
		{
			Global.Day.Register(day =>
				{
					if (day - UpdateDay >= 5)
					{
						UpdateDay = day;
						mSystem.GetRandShopItems();
					}

					if (day - ResetShopItemDataDay >= 10)
					{
						ResetShopItemDataDay = day;
						mSystem.ResetShopItemData();
					}
				})
				.UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		private void OnDestroy()
		{
			mSystem = null;
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}
